package com.xiangyu.demo.controller;

import com.xiangyu.demo.bean.ClientMessage;
import com.xiangyu.demo.bean.ServerMessage;
import com.xiangyu.demo.service.WebSocketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.user.SimpUser;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: springboot-demo
 * @Package: com.xiangyu.demo.controller
 * @Description: java类作用描述
 * @Author: 向羽
 * @CreateDate: 2018/2/1 16:37
 * @UpdateUser: 向羽
 * @UpdateDate: 2018/2/1 16:37
 * @UpdateRemark: The modified content
 * @Version: 2.0
 */
@Controller
public class WebSocketAction {
    private Logger logger = LoggerFactory.getLogger(WebSocketAction.class);
    @Autowired
    private WebSocketService webSocketService;
    @Autowired
    private SimpUserRegistry userRegistry;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @MessageMapping("/sendTest")
    @SendTo("/topic/subscribeTest")
    public ServerMessage sendDemo(ClientMessage message) {
        logger.info("接收到了信息" + message.getName());
        return new ServerMessage("你发送的消息为:" + message.getName());
    }

    @SubscribeMapping("/subscribeTest")
    public ServerMessage sub() {
        logger.info("XXX用户订阅了我。。。");
        return new ServerMessage("感谢你订阅了我。。。");
    }

    @MessageMapping("/welcome")//@MessageMapping和@RequestMapping功能类似，用于设置URL映射地址，浏览器向服务器发起请求，需要通过该地址。
//    @SendTo("/topic/getResponse")//如果服务器接受到了消息，就会对订阅了@SendTo括号中的地址传送消息。
    public ServerMessage say(ClientMessage message) throws Exception {
        List<String> users = new ArrayList();
        users.add("d892bf12bf7d11e793b69c5c8e6f60fb");//此处写死只是为了方便测试,此值需要对应页面中订阅个人消息的userId
        webSocketService.send2Users(users, new ServerMessage("admin hello"));

        return new ServerMessage("Welcome, " + message.getName() + "!");
    }

    @SubscribeMapping("/getResponse")
    public ServerMessage getResponse(ServerMessage serverMessage) {
        logger.info("如何发送给单个订阅的用户");
        return serverMessage;
    }
    @SubscribeMapping(value = "/dsfsdfds")
    public void tempateTest(){
        List<String> users = new ArrayList();
        webSocketService.send2Users(users, new ServerMessage("admin hello"));
    }



    @RequestMapping(value = "/templateTest")
    public void templateTest() {
        logger.info("当前在线人数:" + userRegistry.getUserCount());
        int i = 1;
        for (SimpUser user : userRegistry.getUsers()) {
            logger.info("用户" + i++ + "---" + user);
        }
        //发送消息给指定用户
        messagingTemplate.convertAndSendToUser("test", "/queue/message", new ServerMessage("服务器主动推的数据"));
    }


}
